package com.wd.amodel;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.wd.amodel.adapter.TabLinkageIndicatorAdapter;
import com.wd.amodel.adapter.TabLinkageRvAdapter;
import com.wd.amodel.bean.LinkageIndicatorInfo;
import com.wd.amodel.bean.LinkageInfo;
import com.wd.annotation.Route;
import com.wd.base.config.Constance;
import com.wd.base.util.ViewUtils;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;

import java.util.ArrayList;
import java.util.List;

/**
 * NA<BR>
 *
 * @author w4425
 * @version [V2.0.0, 2021/4/26]
 * @since V2.0.0
 */
@Route(path = Constance.MODULE_URL_DEMO_TAB_LINKAGE)
public class RvTabLinkageActivity extends FragmentActivity {
    private MagicIndicator mIndicator;
    private RecyclerView mRv;
    private TabLinkageRvAdapter mAdapter;
    private List<LinkageInfo> data = new ArrayList<>();
    private List<LinkageIndicatorInfo> mChannels = new ArrayList<>();
    private FragmentContainerHelper mChannelHelper;
    private LinearLayoutManager mLayoutManager;
    private TabLinkageIndicatorAdapter indicatorAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_linkage);
        initView();
        setAdapter();
        loadData();
    }

    private void loadData() {
        for (int i = 0; i < 7; i++) {
            LinkageInfo info = new LinkageInfo();
            info.itemType = 1;
            info.title = "分类标签" + "-" + i;
            info.content = "content " + i;
            data.add(info);
            LinkageIndicatorInfo info1 = new LinkageIndicatorInfo();
            info1.title = "分类标签" + "-" + i;
            mChannels.add(info1);
        }
        refreshIndicator();
        mAdapter.setNewData(data);
    }


    private void refreshIndicator() {
        indicatorAdapter = new TabLinkageIndicatorAdapter(mChannels);
        indicatorAdapter.callbackActivity(new TabLinkageIndicatorAdapter.Callback() {
            @Override
            public void setOnTextChange(int index) {
                mChannelHelper.handlePageSelected(index);
                mLayoutManager.scrollToPositionWithOffset(index, 0);
            }
        });
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(indicatorAdapter);
        mIndicator.setNavigator(commonNavigator);
        mChannelHelper.attachMagicIndicator(mIndicator);
        mChannelHelper.handlePageSelected(0, false);
    }

    private void initView() {
        mIndicator = ViewUtils.findViewById(this, R.id.ingredients_info_indicator);
        mRv = ViewUtils.findViewById(this, R.id.ingredients_info_rv);
    }

    public void setAdapter() {
        mLayoutManager = new LinearLayoutManager(this);
        mChannelHelper = new FragmentContainerHelper();
        mRv.setLayoutManager(mLayoutManager);
        if (mAdapter == null) {
            mAdapter = new TabLinkageRvAdapter(this, data);
        }
        mRv.setAdapter(mAdapter);
        mRv.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                int firstPosition = mLayoutManager.findFirstVisibleItemPosition();
                mChannelHelper.handlePageSelected(firstPosition, false);
            }
        });
    }

}
